import 'package:flutter/material.dart';
import 'package:wk_weather/bean/EnventChangeBean.dart';
import 'package:wk_weather/bean/CityInfo.dart';
import 'package:wk_weather/utils/ColorUtils.dart';
import 'package:wk_weather/utils/SharedPreferenceTool.dart';
import 'package:wk_weather/bean/CityData.dart';
import 'package:wk_weather/views/MyApp.dart';
import 'package:wk_weather/views/pages/search/SearchAppBar.dart';

class HotCityView extends StatefulWidget {
  final CityItemClickCallback itemClickCallback;

  HotCityView({Key key, this.itemClickCallback}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return  _HotCityViewState();;
  }

}

class _HotCityViewState extends State<HotCityView> {
  List<CityData> _cityData;
  List<CityModel> cityModels;

  Widget _buildHotItem(BuildContext context, CityData cityData) {
    return Container(
      margin: EdgeInsets.only(left: 8, right: 8, top: 5, bottom: 5),
      child: ActionChip(
        padding: EdgeInsets.only(left: 16, right: 16, top: 10, bottom: 10),
        label: Text(
          "${cityData.name}",
          style: TextStyle(color: isAdd(cityData) ? Colors.blue : Colors.black),
        ),
        backgroundColor: ColorUtils.parseColorStrToColor("#11000000"),
        onPressed: () async {
          if (isAdd(cityData)) {
            eventBus.fire(ChangeCityEvent(cityData.center));
            Navigator.of(context).pop();
          } else if (widget.itemClickCallback != null) {
            widget.itemClickCallback(cityData);
          }
        },
      ),
    );
  }

  bool isAdd(CityData cityData) {
    bool isAdd = false;
    if (cityModels != null && cityModels.isNotEmpty && cityData != null) {
      cityModels.forEach((element) {
        if (element.cityFlag == cityData.center) {
          isAdd = true;
        }
      });
    }
    return isAdd;
  }

  Widget _buildHotView() {
    if (_cityData != null) {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            margin: EdgeInsets.only(left: 12),
            child: Text(
              "热门城市",
              style: TextStyle(color: ColorUtils.parseColorStrToColor("#bb000000")),
            ),
          ),
          SizedBox(
            height: 10,
          ),
          Wrap(
            alignment: WrapAlignment.start,
            children: _cityData.map((e) => _buildHotItem(context, e)).toList(),
          )
        ],
      );
    } else {
      return null;
    }
  }

  Future<void> fetchHotData() async {
    _cityData = [
      CityData("广州市", "113.280637,23.125178"),
      CityData("佛山市", "113.122717,23.028762"),
      CityData("苏州市", "120.619585,31.299379"),
      CityData("长沙市", "112.982279,28.19409"),
      CityData("天津市", "117.190182,39.125596"),
    ];
    cityModels = await SharedPreferenceTool.getCityModels();
    setState(() {});
  }

  @override
  void initState() {
    fetchHotData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: _buildHotView(),
    );
  }
}
